//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.neurosky.thinkgear;

import android.util.Log;
import com.neurosky.thinkgear.FFT;
import com.neurosky.thinkgear.FFTResult;
import com.neurosky.thinkgear.HanningWindow;
import java.util.Arrays;

public class EnergyLevel {
    private int[] a;
    private int b = 128;
    private int[] c;
    private int[] d;
    private float[] e;
    private float[] f;
    private float[] g;
    private float[] h;
    private float[] i;
    private int[] j;
    private int k = 0;
    private int l = 0;
    private int m = 0;
    private int n = 2;
    private float o;
    private float p;
    private float q;
    private float r;
    private float s;
    private FFT t;
    private FFTResult u;
    private HanningWindow v;
    private int[] w;
    private float[] x;

    public EnergyLevel() {
        this.c = new int[this.b];
        this.d = new int[this.b];
        this.e = new float[this.b];
        this.h = new float[this.b];
        this.i = new float[this.b];
        this.t = new FFT();
        this.v = new HanningWindow(this.b);
        this.j = new int[300];
    }

    public int calculateEnergyLevel(int[] var1, int var2) {
        this.a = new int[var2];
        this.a[0] = 0;

        int var3;
        for(var3 = 1; var3 < var2; ++var3) {
            this.a[var3] = this.a[var3 - 1] + var1[var3];
        }

        if(this.a[var2 - 1] < '\uf80c') {
            return -1;
        } else {
            for(var3 = 0; var3 < this.b; ++var3) {
                this.c[var3] = var3 * 1000 / 2;
            }

            this.w = new int[var1.length];

            for(var3 = 0; var3 < var1.length; ++var3) {
                this.w[var3] = var1[var3];
            }

            for(var3 = 7; var3 < var1.length - 1; ++var3) {
                this.x = new float[]{(float)var1[var3 - 7], (float)var1[var3 - 6], (float)var1[var3 - 5], (float)var1[var3 - 4], (float)var1[var3 - 3], (float)var1[var3 - 2], (float)var1[var3 - 1]};
                if((double)var1[var3] > 1.4D * (double)a(this.x) || (double)var1[var3] < 0.6D * (double)a(this.x)) {
                    this.w[var3] = (int)((double)((float)var1[var3 + 1] + (float)var1[var3 - 1]) / 2.0D);
                }
            }

            this.d[0] = this.w[0];
            var3 = 1;
            int var4 = this.d[0];

            for(var2 = 1; var2 < this.b; ++var2) {
                while(this.a[var3] <= this.c[var2]) {
                    ++var3;
                }

                this.d[var2] = (int)((float)this.w[var3 - 1] + (float)((this.w[var3] - this.w[var3 - 1]) * (this.c[var2] - this.a[var3 - 1])) / (float)this.w[var3]);
                var4 += this.d[var2];
            }

            this.m = var4 / this.b;

            for(var2 = 0; var2 < this.b; ++var2) {
                this.e[var2] = (float)(this.d[var2] - this.m);
            }

            this.e = this.v.applyCoeffs(this.e);
            this.u = this.t.calculateFFT(this.e, this.i, 1, this.b);
            this.f = this.u.getReal();
            this.g = this.u.getImaginary();

            for(var2 = 0; var2 < this.b; ++var2) {
                this.h[var2] = 2.0F * (float)(Math.pow((double)this.f[var2], 2.0D) + Math.pow((double)this.g[var2], 2.0D));
            }

            this.p = 0.0F;
            this.q = 0.0F;

            for(var2 = 0; var2 < this.b; ++var2) {
                this.o = (float)this.n / (float)this.b * (float)var2;
                if((double)this.o >= 0.15D && (double)this.o <= 0.4D) {
                    this.p += this.h[var2];
                }

                if((double)this.o >= 0.04D && (double)this.o <= 0.15D) {
                    this.q += this.h[var2];
                }
            }

            this.r = (float)Math.min(10.0D, (double)(this.q / this.p));
            this.s = (float)(100.0D - 10.0D * (double)this.r);
            this.s = (float)Math.floor((double)this.s + 0.5D);
            if(this.s == 0.0F) {
                this.s = 1.0F;
            }

            return (int)this.s;
        }
    }

    public int addInterval(int var1, int var2) {
        if(var2 == 200) {
            if(this.k >= 300) {
                Log.v("TGDevice", "Buffer reset: " + this.k + " " + this.l);
                this.k = 0;
                this.l = 0;
                this.j = new int[300];
                return 0;
            }

            this.j[this.k++] = var1;
            this.l += var1;
            if(this.l >= 75000) {
                var1 = 0;

                for(var2 = 0; var1 < 5000; ++var2) {
                    var1 += this.j[var2];
                }

                int[] var3 = new int[this.k - var2];
                System.arraycopy(this.j, var2, var3, 0, var3.length);
                var1 = this.calculateEnergyLevel(var3, var3.length);
                this.k = 0;
                this.l = 0;
                return var1;
            }
        } else {
            this.k = 0;
            this.l = 0;
            this.j = new int[300];
        }

        return 0;
    }

    private static float a(float[] var0) {
        Arrays.sort(var0);
        float var1 = (float)((double)((float)var0.length) / 2.0D);
        return var0.length % 2 == 0?(float)((double)(var0[(int)var1 - 1] + var0[(int)var1]) / 2.0D):var0[(int)Math.floor((double)var1)];
    }
}
